<template>
	<view class="content">
		<view class="close-box" @click="closeAddRecep">
			<image src="../../static/images/close-01.png" mode="aspectFill"></image>
		</view>
		<view class="info-add-box">
			<view class="gd-info">
				<image class="img" :src="goodInfo.imgs ? goodInfo.imgs : goodsImg" mode="aspectFill"></image>
				<view class="g-info">
					<view class="g-com g-tit">{{goodInfo.title}}</view>
					<view class="g-com g-s-u-txt">{{goodInfo.gd_brand}}</view>
					<view class="g-price-txt">{{goodInfo.price}}</view>
				</view>
			</view>
			
			<view class="s-list-box">
				<view class="s-info">
					<view>{{allStoreTxt}}</view>
					<view>{{storeInfo.qty_min_total_str}}</view>
				</view>
				<view class="s-list" @click="onClickOpenStroe" v-if="storeList && storeList.length > 0">
					<view class="l-box" v-for="(item,index) in storeList" :key="index">
						<view>{{item.store_name}}</view>
						<view>{{item.qty_min_str}}</view>
					</view>
					<view class="a-box">
						<text class="icon-font icon-style">&#xe600;</text>
					</view>
				</view>
			</view>
			
			<view class="price-i-box">
				<view class="p-box" v-show="showPrePriceTip">
					<view class="p-b-p-s"> 
						<view class="p-pre-txt">上次
							<text class="p-p-s">{{gdPriceI.preTime}}</text>
							<text class="p-p-s">{{gdPriceI.prePrice + "/" + gdPriceI.preUtil}}</text>  
							<text class="p-p-s">x</text>
							<text class="p-p-s">{{gdPriceI.preNum}}</text>
						</view>
					</view>
				</view>
				
				<view class="p-box">
					<view class="p-b-p-s"> 
						<view class="p-i-tit price-i">{{curPriLev.title}}</view>
						<input class="p-i-p" :value="price" type="number" :disabled="!limitChangePrice" @input="inputPrice"/>
						<text class="p-i-u">{{this.unitList[this.selectUnitIdx] ? this.unitList[this.selectUnitIdx].title : ""}}</text>
					</view>
				</view>
				<!--  -->
				<view class="p-box no-line-p-box" v-show="setSellData.is_zk_sku == 1">
					<view class="p-b-p-s"> 
						<view class="p-i-tit">折扣 (%)</view>
						<input class="p-i-p" 
							:disabled="!limitChangePrice"
							:value="discout" 
							type="number" 
							@input="inputDiscount"/>
						<text class="p-i-u">%</text>
					</view>
				</view>
			</view>
			<view class="spec-unit-box comm-list" id="specBox" :style="{height : `calc(100vh - ${boxHeight}px)`}">
				<view class="s-u-b com-s-xScroll">
					<view class="s-l-1" v-for="(item,index) in specList" @click="onClickFirstSpec(item,index)">
						<view class="s-tit-box" >
							<view class="s-tit" :class="selectSpLevel_1 == index ? 'active' :''">{{item.title}}</view>
							<view class="s-tit-n" v-show="item.number > 0">
								<text class="s-tit-n-txt">{{item.number}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="s-u-b com-s-xScroll" v-show="specList_2 && specList_2.length">
					<view class="s-l-2" v-for="(item,index) in specList_2" @click="onClickSecondSpec(item,index)">
						<view class="s-tit" :class="selectSpLevel_2 == index ? 'active2' :''">{{item.title}}
							<view class="s-tit-n" :class="selectSpLevel_2 == index ? 's-active' :''" v-show="item.number > 0">
								<text class="s-tit-n-txt" >{{item.number}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="s-u-b com-s-xScroll" v-show="specList_3 && specList_3.length">
					<view class="s-l-2" v-for="(item,index) in specList_3" @click="onClickThirdSpec(item,index)">
						<view class="s-tit" :class="selectSpLevel_3 == index ? 'active2' :''">{{item.title}}
							<view class="s-tit-n" :class="selectSpLevel_3 == index ? 's-active' :''" v-show="item.number > 0">
								<text class="s-tit-n-txt">{{item.number}}</text>
							</view>
						</view>
					</view>
				</view>
				
				<view class="unit-box">
					<view class="u-l" v-for="(item,index) in unitList" @click="onClickUnit(item,index)" :class="selectUnitIdx == index ? 'active' :''">
						<view class="u-t-box" :class="selectUnitIdx == index ? 'active2' :''">
							<text class="u-tit">{{item.title}}</text>
							<numberBox :ref="'unit_' + item.unit_id" class="s-u-num"
								@eventChange="eventChange"
								:value="item.number"
								:step="step"
								:num_p="decimalPlaces"
								:min="buyMin"
								:input_width="80">
							</numberBox>
						</view>
					</view>
				</view>
				
				<view class="s-u-bz" v-show="setSellData.is_txt == 1">
					<text>备注：</text>
					<input type="text" 
						placeholder-style="font-size:15px;color:#ccc" 
						:placeholder="discPlace" 
						:value="bzInput"
						@input="inputDisc"/>
				</view>
				
			</view>
		</view>
	    <addPurchaseBtns  class="bottom-btns-box"
			:number="totalNumber" 
			:totalPrice="totalPrice"
			@optionAct="optionAct">
		</addPurchaseBtns>
		<popGdStoreWin ref="popGdStoreWin"></popGdStoreWin>
	</view>
</template>

<script>
	import staticConst from "../../js/const/StaticConst.js";
	import https from "../../js/https/Https.js";
	import numberBox from "../comm/uni-number-box.vue";
	import addPurchaseBtns from "./AddPurchaseBtns.vue";
	import popGdStoreWin from "../comm/PopGdStoreWin.vue";
	import util from "../../js/util/Util.js";
	import lang from "../../js/conf/Lang.js";
	const DISCOUT_NUM = 100;
	
	export default {
		components:{numberBox,addPurchaseBtns,popGdStoreWin},
		props:["setSellData","limitChangePrice"],
		data() {
			return {
				buyMin : 1,//最小起订量，
				
				step:1,//加购不长
				
				decimalPlaces : 2,//小数位，
				
				goodsImg:staticConst.GOOD_IMG,
				
				boxHeight:366,
				
				showPrePriceTip : false,
				gdPriceI:{
					preTime : "",
					prePrice : 0,
					preUtil : "",
					preNum : 0,
					priceI : 0,
					discount:100, //折扣
				},
				
				priceList : [], //价格档
				
				suppId:"",
				goodInfo:{},
				priceIState:0,
				priceLevelArr:[], //价格档
				curPriLev:{title:"采购价"}, // 当前选中价格单
				price:0, //输入价格
				
				specList : [], //一级规格，
				selectSpLevel_1 : -1, //一级规格的规格索引
				
				specList_2:[], //二级规格
				selectSpLevel_2 : -1, //二级规格的规格索引
				
				specList_3:[], //三级规格
				selectSpLevel_3 : -1, //三级规格的规格索引
				
				unitList : [], //单位，
				selectUnitIdx : -1, //	
				
				discout : 100, // 折扣
				
				discPlace : "请输入下单备注",
				
				bzInput : "",
				
				totalNumber : 0,
				
				totalPrice : 0,
				
				numPriceDiscountObj : {}, //数量对象,产品价格存储对象,折扣存储对象
				
				changePricDisObj : {}, //改变每个sku产品的价格和折扣存储数据
				
				settingData:null,
				
				cartInfo:null,
				
				storeList:[],
				storeInfo:{},
				allStoreTxt: lang.allStoreTxt,
				
			};
		},
		methods:{
			
			/*** 设置  #imgsBox 的高度***/
			async getBoxHeight()
			{
				const query = await uni.createSelectorQuery().in(this);
				await query.select('#specBox').boundingClientRect(data => {
					this.boxHeight = data.top + 30;
				}).exec();
			},
			
			/***改变价格档****/
			changePriceI(item)
			{
				this.curPriLev = item;
				this.getPriceIBySetPrice();
				this.inputPriIDiscCount();
			},
			
			/****输入价格*****/
			inputPrice(event)
			{
				this.price = event.target.value;
				this.inputPriIDiscCount();
			},
			
			/****输入折扣**0-100**/
			inputDiscount(event)
			{
				let value = event.target.value;
				if(value > DISCOUT_NUM)
				{
					this.discout = DISCOUT_NUM;
					this.$msg({title:lang.receiptAct.inputTipTxt});
					return;
				}
				this.discout = value;
				
				this.inputPriIDiscCount();
			},
			
			/******改变价格档，价格，折扣，重新计算总数********/
			inputPriIDiscCount()
			{
				let key = this.getAddGdSpUnKey();
				
				if(!this.changePricDisObj[key])
				{
					this.changePricDisObj[key] = {};
				}
				this.changePricDisObj[key].discout = this.discout;
				
				if(this.curPriLev && this.curPriLev.id)
				{
					this.changePricDisObj[key].curPriLev = this.curPriLev;
				}
				if(this.numPriceDiscountObj[key])
				{
					let num = this.numPriceDiscountObj[key].number;
					this.addInCart(num);
				}
			},
			
			
			/****输入备注***/
			inputDisc(event)
			{
				this.bzInput = event.target.value;
				let key = this.getAddGdSpUnKey();
				if(!this.numPriceDiscountObj[key])
				{
					this.numPriceDiscountObj[key] = {};
				}
				this.numPriceDiscountObj[key].txt = this.bzInput; //备注
			},
			
			/******选择是否有改过价格折扣*****/
			getChangePriceAndDiscount()
			{
				let key = this.getAddGdSpUnKey();
				let data = this.changePricDisObj[key];
				if(data)
				{
					this.price = this.changePricDisObj[key].price ? this.changePricDisObj[key].price : 0;
					this.discout = this.changePricDisObj[key].discout ? this.changePricDisObj[key].discout : DISCOUT_NUM;
					this.curPriLev = this.changePricDisObj[key].curPriLev;
					if(this.suppId != "" && this.curPriLev.isprice_pre == 1)
					{
						this.goodsPrice();
					}
					return true;
				}
				return false;
			},
			
			/****初始化产品加购信息****/
			async initGdInfo(data)
			{
				this.clearData();
				this.goodInfo = data;
				await this.setSellConfig();
				await this.getGoodsSpec(this.goodInfo.id);
			},
			
			/****编辑购物产品****/
			async editCart(data)
			{
				this.clearData();
				this.goodInfo = data.gdInfo;
				this.cartInfo = data.cartInfo;
				await this.setSellConfig();
				await this.getGoodsSpec(this.goodInfo.id,data.cartInfo);
			},
			
			/****设置编辑购物车初始数据*****/
			setEditCartData(data)
			{
				this.price = data.price_0;
				this.discout = data.discount_r;
				let sp1 = -1;
				let sp2 = -1;
				let sp3 = -1;
				let uId = data.unitid;
				this.getCartUtilIdx(uId);
				
				this.selectSpLevel_1 = -1;
				this.selectSpLevel_2 = -1;
				this.selectSpLevel_3 = -1;
				
				if(data.spec_dir)
				{
					let spArr = String(data.spec_dir).split(",");
					if(spArr.length == 1 && spArr[0] != "")
					{
						sp1 = spArr[0];
						sp2 = data.specid;
					}
					
					if(spArr.length == 2)
					{
						sp1 = spArr[0];
						sp2 = spArr[1];
						sp3 = data.specid;
					}
				}
				else
				{
					sp1 = data.specid;
				}
				if(sp1 != -1)
				{
					for(let i = 0; i<this.specList.length; i++)
					{
						let obj = this.specList[i];
						if(obj.id == sp1)
						{
							this.selectSpLevel_1 = i;
							break;
						}
					}
					
					if(sp2 != -1)
					{
						let obj2 = this.specList[this.selectSpLevel_1];
						let list = obj2.list ? obj2.list : [];
						this.specList_2 = list;
						for(let i = 0; i<list.length; i++)
						{
							let obj = list[i];
							if(obj.id == sp2)
							{
								this.selectSpLevel_2 = i;
								break;
							}
						}
						
						if(sp3 != -1)
						{
							let obj3 = this.specList_2[this.selectSpLevel_2];
							let list = obj3.list ? obj3.list : [];
							this.specList_3 = list;
							for(let i = 0; i<list.length; i++)
							{
								let obj = list[i];
								if(obj.id == sp3)
								{
									this.selectSpLevel_3 = i;
									break;
								}
							}
						}
					}
				}
				let key = this.getAddGdSpUnKey();
				
				this.price = data.price;
				this.changePricDisObj[key] = {};
				this.changePricDisObj[key].discout = this.discout;
				this.changePricDisObj[key].price = this.price;
				this.changePricDisObj[key].curPriLev = this.curPriLev;
				
				this.numPriceDiscountObj[key] = {};
				this.numPriceDiscountObj[key].txt = data.txt;
				this.numPriceDiscountObj[key].id = data.id;
				this.numPriceDiscountObj[key].number = data.number;
				this.numPriceDiscountObj[key].price_0 = data.price_0;
				this.numPriceDiscountObj[key].discount_r = data.discount_r;
				this.numPriceDiscountObj[key].price_i = data.price_i;
				
				this.numPriceDiscountObj[key].randstr = data.randstr;
				this.numPriceDiscountObj[key].spec_dir = data.spec_dir;
				this.numPriceDiscountObj[key].specid = data.specid;
				this.numPriceDiscountObj[key].unitid = data.unitid;
				
				this.addInCart(data.number);
				setTimeout(this.resetUnitNumber,100);
			},
			
			/*****获取单位****/
			getCartUtilIdx(uId)
			{
				let len = this.unitList.length;
				for(let i = 0; i<len; i++)
				{
					let obj = this.unitList[i];
					if(obj.unit_id == uId)
					{
						this.selectUnitIdx = i;
						this.setBzTxt();
						break;
					}
				}
			},
			
			
			/****加购配置信息****/
			setSellConfig()
			{
				if(this.goodInfo && this.goodInfo.buymin)
				{
					let min = util.getInt(this.goodInfo.buymin);
					if(min>0)
						this.buyMin = min;
				}
				else
				{
					if(this.setSellData && this.setSellData.num_min > 0)
						this.buyMin = util.getInt(this.setSellData.num_min);
				}
				
				if(this.setSellData && this.setSellData.num_step)
					this.step = util.getInt(this.setSellData.num_step);
				if(this.setSellData  && this.setSellData.num_p)
					this.decimalPlaces = this.setSellData.num_p;
			},
			
			/****清除数据****/
			clearData()
			{
				this.getBoxHeight();
				this.showPrePriceTip = false;
				
				this.priceList = [];
				this.priceLevelArr = [];
				this.specList = [];
				
				this.price = 0;
				
				this.specList = []; //一级规格，
				this.selectSpLevel_1 = -1; //一级规格的规格索引
				
				this.specList_2 = []; //二级规格
				this.selectSpLevel_2 = -1; //二级规格的规格索引
				
				this.specList_3 = []; //三级规格
				this.selectSpLevel_3 = -1; //三级规格的规格索引
				
				this.numPriceDiscountObj = {};//数量，价格，折扣
				
				this.changePricDisObj = {};//输入更改的价格和折扣，
				
				this.gdPriceI = {
					preTime : "",
					prePrice : 0,
					preUtil : "",
					preNum : 0,
					priceI : 0,
					discount:100, //折扣
				};
				
				this.unitList = [];
				this.selectUnitIdx = -1; //	
				this.bzInput = "";
				
				this.totalNumber = 0;
				this.totalPrice = 0;
				
				this.price_type_gd = 0;  //["0无关联", "1按单位报价", "2按规格报价"]
				
				this.settingData = null;  //[price_type_gd : "0无关联", "1按单位报价", "2按规格报价"]
				
				this.cartInfo = null;
			},
			
			/****关闭****/
			closeAddRecep()
			{
				this.$emit("closeAddRecep");
			},
			
			/****获取规格单位， key值****/
			getAddGdSpUnKey()
			{
				if(!this.goodInfo)return;
				if(!this.unitList || this.unitList.length == 0)return;
				let unitId = this.unitList[this.selectUnitIdx].unit_id;
				let key = this.goodInfo.id;
				if(this.selectSpLevel_1 > -1)//一级规格
				{
					key = key + "|" + this.specList[this.selectSpLevel_1].id;
					if(this.selectSpLevel_2 > -1)//二级规格
					{
						key = key + "_" + this.specList_2[this.selectSpLevel_2].id;
						if(this.selectSpLevel_3 > -1)//三级规格
						{
							key = key + "_" + this.specList_3[this.selectSpLevel_3].id;
						}
					}
				}
				else
				{
					key = key + "|0"  //无规格
				}
			   
				key = key + "|" + unitId;
				return key;
			},
			
			/****点击一级规格****/
			onClickFirstSpec(item,index)
			{
				if(this.selectSpLevel_1 == index)return;
				this.selectSpLevel_2 = -1;
				this.selectSpLevel_3 = -1;
				this.specList_2 = [];
				this.specList_3 = []; //三级规格
				this.selectUnitIdx = 0;
				this.setFirstSpecIdx(index);
				this.getGoodsPriceAndPriceI();
				this.resetUnitNumber(); //清零输入数据
			},
			
			getGoodsPriceAndPriceI()
			{
				if(!this.getChangePriceAndDiscount())
				{
					if(this.suppId)
					{
						this.goodsPrice();
					}
					else
					{
						this.selectPriceI(false);
					}
				}
			},
			
			/****选中了第一级规格***/
			setFirstSpecIdx(idx)
			{
				this.selectSpLevel_1 = idx;
				let obj1 = this.specList[this.selectSpLevel_1];
				if(obj1 && obj1.list)
				{
					this.specList_2 = obj1.list;
					this.selectSpLevel_2 = 0;
					let obj2 = this.specList_2[this.selectSpLevel_2]; 
					if(obj2 && obj2.list)
					{
						this.specList_3 = obj2.list;
						this.selectSpLevel_3 = 0;
					}
				}
			},
			
			/****选中了第二级规格***/
			setSecondSpecIdx(idx)
			{
				this.selectSpLevel_2 = idx;
				let obj2 = this.specList_2[this.selectSpLevel_2]; 
				if(obj2 && obj2.list)
				{
					this.specList_3 = obj2.list;
					this.selectSpLevel_3 = 0;
				}
			},
			
			/****点击二级规格****/
			onClickSecondSpec(item,index)
			{
				if(this.selectSpLevel_2 == index)return;
				this.selectSpLevel_3 = -1;
				this.specList_3 = []; //三级规格
				this.selectUnitIdx = 0;
				this.setSecondSpecIdx(index);
				this.getGoodsPriceAndPriceI();
				this.resetUnitNumber(); //清零输入数据
			},
			
			/****点击三级规格****/
			onClickThirdSpec(item,index)
			{
				if(this.selectSpLevel_3 == index)return;
				this.selectSpLevel_3 = index;
				this.selectUnitIdx = 0;
				this.getGoodsPriceAndPriceI();
				this.resetUnitNumber(); //清零输入数据
			},
			
			/*****点击单位****/
			onClickUnit(item,index)
			{
				if(this.selectUnitIdx == index)return;
				this.selectUnitIdx = index;
				this.getGoodsPriceAndPriceI();
				this.setBzTxt();
			},
			
			setBzTxt()
			{
				let key = this.getAddGdSpUnKey();
				this.bzInput = this.numPriceDiscountObj[key] && this.numPriceDiscountObj[key].txt ? this.numPriceDiscountObj[key].txt : "";
			},
			
			/****点击客户，设置客户id****/
			setCustom(id)
			{
				this.suppId = id;
				if(this.goodInfo && this.goodInfo.id)
					this.getGoodsSpec(this.goodInfo.id);
			},
			
			/****产品规格****/
			getGoodsSpec(id,cartInfo=null)
			{
				let that = this;
				let param = {};
				param.goods_id = id;
				param.suppid = this.suppId;
				param.ch = 2,//staticConst.ORDER_CG;
				https.getGoodsSpec(param,back);
				function back(data)
				{
					if(!data)return;
					if(data.status == 1)
					{
						if(!data.data)return;
						data = data.data;
						that.storeList = [];
						if(data.store)
						{
							let store = data.store;
							that.storeInfo.goods_name = store.info.goods_name;
							that.storeInfo.qty_min_total_str = store.info.qty_min_total_str;
							if(store.list)
							{
								for(let i = 0; i < store.list.length; i++)
								{
									if(i > 1)break;
									let temp = store.list[i];
									let temp1 = {};
									temp1.store_name = temp.store_name;
									temp1.qty_min_str = temp.qty_min_str;
									that.storeList.push(temp1);
								}
								
							}
						}
						
						if(data.price_ls && data.price_ls.gd_price)
						{
							if(data.price_ls.setting) //报价方式
								that.settingData = data.price_ls.setting;
							that.priceLevelArr = [];
							that.priceList = data.price_ls.price_ls;
							let arr = data.price_ls.gd_price;
							let len = arr.length;
							for(let i = 0; i < len; i++)
							{
								let temp = arr[i];
								let obj = {};
								obj.title = temp.title;
								obj.isod = temp.isod; //默认选中价格档
								obj.isprice_pre = temp.isprice_pre; //是否是老客户价
								obj.id = temp.psort;
								obj.show = true;
								that.priceLevelArr.push(obj); //价格档
							}
						}
						else
						{
							that.priceLevelArr = [];
						}
						
						console.log("that.priceLevelArr===========:",that.priceLevelArr);
						
						if(data.spec_ls)
						{//规格
							that.specList = data.spec_ls;
							if(!cartInfo)
								that.setFirstSpecIdx(0); //默认第一个
						} 
						
						
						if(data.unit_ls)
						{//单位
							that.unitList = data.unit_ls;
							that.selectUnitIdx = 0;
						}
						if(that.suppId)
						{
							that.goodsPrice(cartInfo);
						}
						else
						{
							that.selectPriceI(false);
							if(cartInfo)
								that.setEditCartData(cartInfo);
						}
					}
				}
			},
			
			/***获取单一规格单位价格****/
			getSinglePrice(sId,uId,pId)
			{
				let price = 0;
				if(this.priceList)
				{
					let len = this.priceList.length;
					let has = false;
					for(let i = 0; i<len; i++)
					{
						let o = this.priceList[i];
						console.log(o.specid,sId,uId,o.unitid,pId);
						if(o.specid == sId && o.unitid == uId)
						{
							price = util.getInt(o.prices);
							has = true;
						}
						if(has)
						{
							break;
						}
					}
				}
				return price;
			},
			
			/****获取老客户价格***/
			goodsPrice(cartInfo)
			{
				let obj = this.getCurSpecAndUnit();
				if(obj)
					this.getGdPrice(obj.specId,obj.unitId,cartInfo);
			},
			
			/****获取当前选中的规格id和单位id*****/
			getCurSpecAndUnit()
			{
				if(!this.unitList || this.unitList.length == 0)return;
				let specId = 0; //按单位报价
				if(this.settingData && this.settingData.price_type_gd == 2) //按规格报价
				{
					if(this.selectSpLevel_1 > -1)
						specId = this.specList[this.selectSpLevel_1].id;
					if(this.selectSpLevel_2 > -1)
						specId = this.specList_2[this.selectSpLevel_2].id;
					if(this.selectSpLevel_3 > -1)
						specId = this.specList_3[this.selectSpLevel_3].id;
				}
				let obj = {};
				obj.specId = specId;
				if(this.settingData && this.settingData.price_type_gd == 0)
					obj.unitId = 0;
				else
					obj.unitId = this.unitList[this.selectUnitIdx].unit_id;
				console.log("obj===============================:",obj);
				return obj;
			},
			
			/*****开单-获取产品价格（含上次报价****/
			getGdPrice(specId,unitId,cartInfo)
			{
				if(this.suppId == "")return;
				let that = this;
				let param = {};
				param.type_od = 1;
				param.gdid = this.goodInfo.id;
				param.suppid = this.suppId;
				if(cartInfo)
				{
					param.specid = cartInfo.specid;
					param.unitid = cartInfo.unitid;
				}
				else
				{
					param.specid = specId;
					param.unitid = unitId;
				}
				https.cgGdPrice(param,back);
				function back(data)
				{
					if(!data)
					{
						that.showPrePriceTip = false;
						that.getBoxHeight();
						that.selectPriceI(false);
						if(cartInfo)
							that.setEditCartData(cartInfo)
						return;
					}
					
					if(data.status == 1)
					{
						if(!data.data)
						{
							that.showPrePriceTip = false;
							that.getBoxHeight();
							that.selectPriceI(false);
							if(cartInfo)
								that.setEditCartData(cartInfo)
							return;
						}
						//老客户价
						that.showPrePriceTip = true;
						that.getBoxHeight();
						that.gdPriceI.preTime = data.data.atime_txt;
						that.gdPriceI.prePrice = util.getInt(data.data.price);
						that.gdPriceI.preUtil = data.data.unit_title;
						that.gdPriceI.preNum = util.getInt(data.data.number); 
						that.gdPriceI.priceI = data.data.price_i;
						that.gdPriceI.discount = util.getInt(data.data.discount_r);
						that.selectPriceI(true);
						
						if(cartInfo)
							that.setEditCartData(cartInfo)
					}
					else
					{
						that.selectPriceI(false);
						if(cartInfo)
							that.setEditCartData(cartInfo)
					}
				}
			},
			
			/***默认选中价格档，
				如果有老客户价，优先选择老客户价，
				如果没有，默认选择设置的价格档****/
			selectPriceI(isOld=false)
			{
				this.discout = 100;
				if(this.priceLevelArr && this.priceLevelArr.length > 0)
				{
					// let len = this.priceLevelArr.length;
					// let oldIdx = -1;
					// for(let i = 0; i<len; i++)
					// {
					// 	if(this.priceLevelArr[i].isprice_pre == 1)
					// 	{
					// 		oldIdx = i;
					// 		if(isOld)
					// 		{
					// 			oldIdx = i;
					// 			this.curPriLev = this.priceLevelArr[i]; //老客户
					// 			break;
					// 		}
					// 	}
					// 	if(this.priceLevelArr[i].isod == 1)
					// 	{
					// 		if(!isOld)
					// 			this.curPriLev = this.priceLevelArr[i];
					// 	}
					// }
					// console.log(oldIdx,isOld);
					// console.log(this.curPriLev);
					// if(oldIdx > -1)
					// {
					// 	if(!isOld) //清除老客户列表
					// 	{
					// 		let item = this.priceLevelArr[oldIdx];
					// 		item.show = false;
					// 	}
					// 	else
					// 	{
					// 		let item = this.priceLevelArr[oldIdx];
					// 		item.show = true;
					// 	}
					// }
					this.curPriLev = this.priceLevelArr[0];
					if(!this.cartInfo)
						this.getPriceIBySetPrice();
					this.cartInfo = null; //初始化用清空
				}
				else
				{
					this.price = 0;
				}
			},
			
			/***根据价格档显示价格****/
			getPriceIBySetPrice()
			{
				let that = this;
				this.discout = 100;
				console.log("this.curPriLev===================:",this.curPriLev);
				if(this.curPriLev)
				{
					if(this.curPriLev.isprice_pre == 1)
					{
						this.price = this.gdPriceI.prePrice;
						this.discout = this.gdPriceI.discount;
					}
					else
					{
						let obj = this.getCurSpecAndUnit();
						console.log("obj==================:",obj);
						if(obj)
						{
							this.price = this.getSinglePrice(obj.specId,obj.unitId,this.curPriLev.id);
						}
						else
						{
							this.price = 0;
						}
					}
				}
				else
				{
					this.price = 0;
				}
			},
			
			/***添加，减少加购数量********/
			eventChange(data)
			{
				setTimeout(this.addInCart,100,data.number);
			},
			
			/****添加本地购物保存****/
			async addInCart(num)
			{
				let obj = {};
				let gdid = this.goodInfo.id;
				let unitId = this.unitList[this.selectUnitIdx].unit_id;
				
				let specId1 = 0; //无规格
				let specId2 = -1;
				let specId3 = -1;
				
				if(this.selectSpLevel_1 > -1)
					specId1 = this.specList[this.selectSpLevel_1].id;
				if(this.selectSpLevel_2 > -1)
					specId2 = this.specList_2[this.selectSpLevel_2].id;
				if(this.selectSpLevel_3 > -1)
					specId3 = this.specList_3[this.selectSpLevel_3].id;
				
				let data = {};
				data.number = num;//数量
				data.price = this.price;//价格
				data.discount_r = this.discout;//折扣
				data.price_i = this.curPriLev.id;//价格档
				data.txt = this.bzInput;
				data.unitid = unitId;
				if(specId1 > -1 && specId2 > -1 && specId3 > -1)
				{
					let key = gdid + "|" + specId1 + "_" + specId2 + "_" + specId3 + "|" + unitId;
					let temp = this.numPriceDiscountObj[key];
					data.price_0 = this.price;
					data.spec_dir = specId1 + "," + specId2;
					data.specid = specId3;
					if(!temp || !temp.id)
					{
						data.randstr = util.random6Str();
						data.id = "";
						data.txt = "";
					}
					else
					{
						data.randstr = temp.randstr;
						data.id = temp.id;
						data.txt = temp.txt ? temp.txt : "";
					}
					this.numPriceDiscountObj[key] = data;
					
					let obj1 = this.specList[this.selectSpLevel_1];
					let obj2 = this.specList_2[this.selectSpLevel_2];
					let obj3 = this.specList_3[this.selectSpLevel_3];
					
					if(obj1)
						obj1.number = await this.sum(gdid,specId1,-1,-1,unitId);
					if(obj2)
						obj2.number = await this.sum(gdid,specId1,specId2,-1,unitId);
					if(obj3)
						obj3.number = await this.sum(gdid,specId1,specId2,specId3,unitId);
				}
				else if(specId1 > -1 && specId2 > -1 && specId3 == -1)
				{
					let key = gdid + "|" + specId1 + "_" + specId2 + "|" + unitId;
					let  temp = this.numPriceDiscountObj[key];
					data.price_0 = this.price;
					data.spec_dir = specId1;
					data.specid = specId2;
					if(!temp || !temp.id)
					{
						data.randstr = util.random6Str();
						data.id = "";
						data.txt = "";
					}
					else
					{
						data.randstr = temp.randstr;
						data.id = temp.id;
						data.txt = temp.txt ? temp.txt : "";
					}
					//只有二级级规格
					this.numPriceDiscountObj[key] = data;
					let obj1 = this.specList[this.selectSpLevel_1];
					let obj2 = this.specList_2[this.selectSpLevel_2];
					obj1.number = await this.sum(gdid,specId1,-1,-1,unitId);
					if(obj2)
						obj2.number = await this.sum(gdid,specId1,specId2,-1,unitId);
				}
				else
				{
					//只有一级规格
					let key = gdid + "|" + specId1 + "|" + unitId;
					let  temp = this.numPriceDiscountObj[key];
					data.price_0 = this.price;
					data.spec_dir = "";
					data.specid = specId1;
					if(!temp || !temp.id)
					{
						data.randstr = util.random6Str();
						data.id = "";
						data.txt = "";
					}
					else
					{
						data.randstr = temp.randstr;
						data.id = temp.id;
						data.txt = temp.txt ? temp.txt : "";
					}
					this.numPriceDiscountObj[key] = data;
					let obj1 = {};
					if(specId1 > 0)
						obj1 = this.specList[this.selectSpLevel_1];
					obj1.number = await this.sum(gdid,specId1,-1,-1,unitId);
				}
				this.$forceUpdate();
			},
			
			/******统计各个规格的总数，红色点显示的数据******/
			sum(gId,specId1,specId2,specId3,uid)
			{
				let num = 0;
				let totalNum = 0;
				let tPrice = 0;
				for(let k in this.numPriceDiscountObj)
				{
					let tArr = String(k).split("|");
					let spIdsArr = String(tArr[1]).split("_");
					let id = tArr[0];
					let sp1 = spIdsArr[0];
					let sp2 = -1;
					let sp3 = -1;
					if(specId2 > -1)
					{
						sp2 = spIdsArr[1];
					}
					
					if(specId3 > -1)
					{
						sp3 = spIdsArr[2];
					}
					
					let unitId = tArr[2];
					
					if(id == gId && sp1 == specId1 && sp2 == specId2 && sp3 == specId3)
					{
						num = num + this.numPriceDiscountObj[k].number;
					}
					totalNum = totalNum + this.numPriceDiscountObj[k].number;
					tPrice = tPrice + this.numPriceDiscountObj[k].price * this.numPriceDiscountObj[k].number * (this.numPriceDiscountObj[k].discount_r / 100);
				}
				this.totalNumber = totalNum;
				this.totalPrice = util.decimalPlaceCount(tPrice,this.decimalPlaces);
				return num;
			},
			
			/****输入框数据清零****/
			resetUnitNumber()
			{
				let len = this.unitList.length;
				for(let i = 0; i<len; i++)
				{
					let obj = this.unitList[i];
					let id = obj.unit_id;
					if(this.$refs["unit_" + id] && this.$refs["unit_" + id][0])
					{
						let num = this.getNumberGdidSpecIdsUnit(id);
						this.$refs["unit_" + id][0].setValue(num);
					}
				}
				this.setBzTxt();
			},
			
			/****获取数据***/
			getNumberGdidSpecIdsUnit(unitId)
			{
				let gdId = this.goodInfo.id;
				let specIds = 0;
				if(this.selectSpLevel_1 > -1)
					specIds = this.specList[this.selectSpLevel_1].id;
				if(this.selectSpLevel_2 > -1)
				{
					specIds = specIds + "_" + this.specList_2[this.selectSpLevel_2].id;
					if(this.selectSpLevel_3 > -1)
						specIds = specIds + "_" + this.specList_3[this.selectSpLevel_3].id;
				}
				let key = gdId + "|" + specIds + "|" + unitId;
				let num = this.numPriceDiscountObj[key] && this.numPriceDiscountObj[key].number ? Number(this.numPriceDiscountObj[key].number) : 0;
				return num;
			},
			
			/****计算价格***/
			optionAct(act)
			{
				if(act == 1)
					this.saveCart();
				else
					this.clearCartData();
			},
			
			/***清除数据****/
			clearCartData()
			{
				this.clearData();
				this.getGoodsSpec(this.goodInfo.id);
			},
			
			/****获取sku列表******/
			getSpecUnitLs()
			{
				let arr = [];
				for(let k in this.numPriceDiscountObj)
				{
					let data = this.numPriceDiscountObj[k];
					if(data.number<=0)continue;
					arr.push(data);
				}
				console.log("arr============:",arr);
				return arr;
			},
			
			/****提交到购物车*****/
			saveCart()
			{
				let that = this;
				let arr = this.getSpecUnitLs();
				if(this.totalNumber == 0 || !arr || arr.length==0)
				{
					let msg = lang.receiptCheckOut.inputTipNumTxt;
					this.$msg({title:msg});
					return;
				}
				let param = {};
				param.type = 1; //开单类型1销售单；2退货单，加购产品时可不传，本参可单独传值
				param.suppid = this.suppId;
				param.gdid = this.goodInfo.id;
				param.spec_unit_ls = arr;
				https.cgCartSave(param,back);
				function back(data)
				{
					if(!data)return;
					if(data.status == 1)
					{
						if(!data.data)return;
						that.$emit("saveCart");
					}
				}
			},
			
			/*****设置客户id为空*******/
			setCustomerIdNull()
			{
				this.suppId = "";
			},
			
			/******打开库存窗口*****/
			onClickOpenStroe()
			{
				let data = {};
				data.id = this.goodInfo.id;
				data.ch = "store";
				this.$refs.popGdStoreWin.show("show",data);	
			}
		}
	}
</script>

<style lang="scss">
	.content
	{
		width: 310px;
		height: 100%;
		position: relative;
		background: #F9F9F9;
		// border-left: #ECECEC 1px solid;
		.bottom-btns-box
		{
			position: fixed;
			right: 0px;
			bottom: -1px;
			width: 310px;
			height: 60px;
		}
		.close-box
		{
			width: 15px;
			height: 15px;
			position: absolute;
			padding: 3px;
			
			top:20px;
			right: 20px;
			cursor: pointer;
			image
			{
				width: 100%;
				height: 100%;
			}
		}
		
		.close-box:hover
		{
			background-color: #EEE;
			opacity: .9;
		}
		.close-box:active
		{
			background-color: #DDD;
			opacity: .8;
		}
		.info-add-box
		{
			padding-top: 100px;
			font-size: 15px;
			.gd-info
			{
				padding: 5px 15px;
				height: 82px;
				display: flex;
				align-items: center;
				.img
				{
					width: 60px;
					height: 60px;
					border-radius: 4px;
					overflow: hidden;
				}
				
				.g-info
				{
					padding-left: 10px;
					.g-com{
						max-width: 200px;
						overflow:hidden;
						white-space:nowrap;
						text-overflow:ellipsis;
					}
					.g-tit
					{
						font-size: 16px;
					}
					.g-s-u-txt
					{
						color: #999;
					}
					.g-price-txt
					{
						color: red;
					}
				}
			}
			
			.s-list-box
			{
				margin-bottom: 10px;
				
				.s-info
				{
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 5px 15px;
				}
			
				.s-list
				{
					color: #333;
					background-color: #FFF;
					cursor: pointer;
					padding: 5px 15px;
					position: relative;
					.l-box
					{
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 245px;
					}
					.a-box
					{
						display: flex;
						align-items: center;
						position: absolute;
						right: 15px;
						height: 100%;
						top: 0;
					}
				}
			}
			
			.price-i-box
			{
				background-color: #fff;
				
				.p-box
				{
					border-bottom: #eee 1px solid;
					.p-b-p-s
					{
						display: flex;
						padding: 5px 15px;
						
						.p-pre-txt
						{
							color: #999;
							.p-p-s
							{
								padding-left:6px ;
							}
						}
						
						.p-i-tit
						{
							width: 110px;
							overflow:hidden;
							white-space:nowrap;
							text-overflow:ellipsis;
						}
						.price-i
						{
							cursor: pointer;
						}
						
						.p-i-p
						{
							font-weight: bold;
							padding-left: 20px;
							width: 115px;
							font-size: 17px;
						}
						.p-i-u
						{
							width: 50px;
							text-align: right;
							overflow:hidden;
							white-space:nowrap;
							text-overflow:ellipsis;
						}
					}
				}
				.no-line-p-box
				{
					border-bottom : none;
				}
			}
			
			.spec-unit-box
			{
				margin-top: 10px;
				background-color: #fff;
				.s-u-b
				{
					padding: 10px 10px;
					display: flex;
					white-space:nowrap;
					width: calc( 100% - 30px);
					border-bottom: #eee 1px solid;
					.s-l-1
					{
						padding: 1px 7px;
						.s-tit-box
						{
							position: relative;
							padding: 0 8px;
							
							.s-tit
							{
								color: #333;
							}
							.active
							{
								color: #3388ff;
								border-bottom: 2px solid #3388ff;
							}
							.s-tit-n
							{
								position: absolute;
								width: auto;
								min-width: 26px;
								height: 26px;
								font-size: 12px;
								top: -12px;
								right: -11px;
								color: #FFF;
								background-color: #ff0000;
								border-radius: 13px;
								transform: scale(.7, .7);
								text-align: center;
								.s-tit-n-txt
								{
									padding: 0 2px;
									line-height: 26px;
									text-align: center;
								}
							}
						}
					}
					.s-l-1:first-child
					{
						padding-left: 0;
					}
					
					
					.s-l-2
					{
						border: #3388ff 1px solid;
						border-radius: 4px;
						margin-right: 8px;
						.s-tit
						{
							position: relative;
							padding: 0 8px;
							color: #3388ff;
							.s-tit-n
							{
								position: absolute;
								width: auto;
								min-width: 26px;
								height: 26px;
								font-size: 12px;
								top: -15px;
								right: -11px;
								color: #FFF;
								background-color: #ff0000;
								border-radius: 13px;
								transform: scale(.7, .7);
								text-align: center;
								.s-tit-n-txt
								{
									padding: 0 2px;
									line-height: 26px;
									text-align: center;
								}
							}
						}
						
						.active2
						{
							color: #fff;
							border: #3388ff 1px solid;
							background-color: #3388ff;
						}
					}
				
				}
				.unit-box
				{
					.u-l
					{
						cursor: pointer;
						padding: 5px 10px;
						border-bottom: #eee 1px solid;
						.u-t-box
						{
							display: flex;
							align-items: center;
							justify-content: space-between;
						}
					}
					
					.active
					{
						background-color: #FAFAFA;
					}
				}
				.s-u-bz
				{
					display: flex;
					padding: 10px 15px;
					align-items: center;
				}
			}
			
		}
	}
	
</style>
